package com.test.controller;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.test.pojo.Cart;
import com.test.pojo.CartInfo;
import com.test.pojo.Users;
import com.test.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    public ICartService getCartService() {
        return cartService;
    }

    public void setCartService(ICartService cartService) {
        this.cartService = cartService;
    }

    @RequestMapping("/addGoodsToCart")
    @ResponseBody
    public int addGoodsToCart(int skuId, int buyNum, HttpServletRequest request)
    {
          HttpSession session= request.getSession();

          if(session.getAttribute("loginUser")!=null) {

              //1.如果登录了，从session中取出cartId
              Users user= (Users)session.getAttribute("loginUser");
              int cartId=user.getCart().getCartId();
              System.out.println(cartId);
              //2.将skuId,buyNum,cartId组装成CartInfo对象
              CartInfo cartInfo=new CartInfo(cartId,skuId,buyNum);
              //3.交给service层处理
              int totalNum= cartService.addGoodsToCart(cartInfo);

              //根据session中的信息
              Cart cart=user.getCart();
              cart.setTotalNum(totalNum);
              user.setCart(cart);

              session.setAttribute("loginUser",user);

              return totalNum;
          }
          else
              return -1;

    }

    @RequestMapping("/deleteGoodsFromCart")
    @ResponseBody
    public int deleteGoodsFromCart(int skuId,HttpServletRequest request)
    {
         //获得cartId
        HttpSession session=request.getSession();

        if(session.getAttribute("loginUser")!=null)
        {
            Users user=(Users) session.getAttribute("loginUser");

            int cartId= user.getCart().getCartId();

            CartInfo cartInfo=new CartInfo(cartId,skuId);

            int totalNum= cartService.deleteGoodsFromCart(cartInfo);

            user.getCart().setTotalNum(totalNum);

            session.setAttribute("loginUser",user);

            return totalNum;
        }
        else
        {
            return  -1;
        }


    }


    @RequestMapping(value = "/showCartList")
    public ModelAndView showCartList(HttpServletRequest request, HttpServletResponse response)
    {
        //查询该购物车的详细信息
        HttpSession session=request.getSession();
        if(session.getAttribute("loginUser")!=null) {

            //先获得cartId
            Users user= (Users) session.getAttribute("loginUser");

            Cart cart=null;

            if(user.getCart().getTotalNum()!=0) {

                int cartId = user.getCart().getCartId();
                System.out.println(cartId);
                //传到service查询完整的购物车信息
                cart= cartService.selectCartList(cartId);
                //将完整的购物车信息存入session
                user.setCart(cart);
                session.setAttribute("loginUser",user);
            }
            else
                cart =user.getCart();

            //转发到shopcart页面
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("cart", cart);
            modelAndView.setViewName("shopcart");
            return modelAndView;
        }
        else
        {
            //没有登录 跳转到登录页面
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;

        }
    }

}
